#!/bin/bash
#author: <fanyuantao>
#date: 2024-08-08
#desc: This script is used to get docker images and export them as tgz files.

output_dir="$PWD/out"

if [ ! -d $output_dir ]; then
    mkdir -p "$output_dir"
fi

function echo_logo() {
    cat << EOF

        ____ ____  _____    _  _____ _____ ____ _   _ 
        / ___|  _ \| ____|  / \|_   _| ____/ ___| | | |
        | |  _| |_) |  _|   / _ \ | | |  _|| |   | |_| |
        | |_| |  _ <| |___ / ___ \| | | |__| |___|  _  |
        \____|_| \_\_____/_/   \_\_| |_____\____|_| |_|
                                                        

EOF
    echo
    echo "================ Thanks for using Script ========================"
    echo ""
    echo -e "\t\t Author:   \033[33m fanyuantao \033[0m \n"
    echo -e "\t\t Release:  \033[33m 2024-07-16 \033[0m \n"
    echo -e "\t\t Update:   \033[33m 2024-08-12 \033[0m \n"
    echo -e "\t\t Version:  \033[33m 1.0.0 \033[0m \n"
    echo ""
    echo "================================================================="
}

# 遍历所有正在运行的容器，将镜像导出并压缩成 tgz 文件，若容器名中包含斜杆与冒号，都需要替换成下划线
function export_running_container_images() {
    for i in `docker ps -a | grep -v IMAGE | awk '{print $2}'`; do echo "exporting $i" && docker save "$i" | gzip > $output_dir/${i//\//_}.tgz | pv -l >/dev/null; done
}

# 遍历 docker images 并导出与压缩成 tgz 文件，若镜像名中包含斜杆与冒号，都需要替换成下划线
function export_all_images() {
    for i in `docker images | grep -v TAG | awk '{print $1":"$2}'`; do echo "exporting $i" && docker save "$i" | gzip > $output_dir/${i//\//_}.tgz | pv -l >/dev/null; done
}

# 遍历目录下的所有文件，将文件名称包含冒号的替换成下划线
function rename_files() {
    for file in `ls $output_dir`; do mv $output_dir/$file $output_dir/${file//:/_}; done
    echo ""
    echo "export success"
}

# 加载服务镜像
function load_images() {
    for file in $output_dir/*.tgz; do docker load -i $file; done
    echo ""
    echo "load success"
}

function echo_menu() {
    while true; do
        echo "=========== Menu ========="
        echo "1. 导出正在运行的容器镜像"
        echo "2. 导出所有镜像"
        echo "3. 加载服务镜像"
        echo "0. 退出"
        echo "=========== Done ========="
        read -p "请输入选项:" num
        case $num in
            1)
                export_running_container_images
                rename_files
                ;;
            2)
                export_all_images
                rename_files
                ;;
            3)
                load_images
                ;;
            0)
                exit 0
                ;;
            *)
                echo "请输入正确的序号！"
                ;;
        esac
    done
}

function main() {
    echo_logo
    echo_menu
}

main

# 因为pv是EPEL存储库的一个软件件，所以我们只需要先安装EPEL库

# 安装pv
# yum install epel-release -y && yum install pv -y
# 或
# sudo apt-get install pv -y

# 清理工作空间
# docker system prune -f

# ${image_name//\//_} 与${image_name//:/_}如何拼在一起？
# 因为${image_name//\//_}会把斜杆替换成下划线，而${image_name//:/_}则会把冒号替换成下划线。
# 所以，如果镜像名中包含斜杆与冒号，则使用 ${image_name//\//_}_${image_name//:/_} 拼接镜像名。
# 例如，镜像名为registry.cn-hangzhou.aliyuncs.com/test/test:v1，则最终导出的镜像名为registry.cn-hangzhou.aliyuncs.com_test_test_v1.tar。